"""
@author: 一切、會恏
@file: do_mysql.py
@time: 2019/3/27 11:26
"""
from mysql import connector
from common.public.read_conf_file import ReadConfFile
from common.public import project_path
class DoMysql:
    '''操作数据库类'''
    def do_mysql(self,query,flag=1):
        '''
        query: sql查询语句
        flag: 控制输出条数
        '''
        db_config = eval(ReadConfFile().readFile(project_path.con_path, "DB", "db_config"))
        conn = connector.connect(**db_config)#以字典的形式传入数据库信息
        cur = conn.cursor()#获取游标，拿到操作数据库的权限
        cur.execute(query)#执行sql查询语句

        if flag ==1:#拿到查询结果，如果flag是1，则拿到一条结果；若flag是2，则拿取所有结果
            res = cur.fetchone()#返回一个元组
        elif flag ==2:
            res = cur.fetchall()#返回嵌套元组的列表
        conn.close()#查询完要记得关闭数据库连接
        return res



if __name__ == '__main__':
    query = "SELECT MAX(Id) FROM loan WHERE MemberID = 36"
    res = DoMysql().do_mysql(query,1)
    print(res)


